<?php

namespace App\Console\Commands;

use App\Models\Base\ServerList;
use App\Service\PlayerService;
use App\Service\ServerOnlineService;
use Illuminate\Console\Command;

class FetchPlyerOnline extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch:online';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计从游戏服务器抓取玩家登陆数量';

    /**
     * [$payer 玩家逻辑类].
     *
     * @var [type]
     */
    protected $payer;
    /**
     * [$serveronline 游戏服务模型].
     *
     * @var [type]
     */
    protected $serveronline;

    /**
     * Create a new command instance.
     */
    public function __construct(ServerOnlineService $serveronline)
    {
        $this->player = new PlayerService();
        $this->serveronline = $serveronline;

        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //获取所有游戏数据库配置
        $list = ServerList::all();
        if (null == $list) {
            return false;
        }
        foreach ($list as $server) {
            //设置数据库配置
            setDB($server->server_id);
            $online_num = PlayerService::getOnlinePlayerNum();
            //写入统计表
            PlayerService::saveLocalPlayerNum($server->server_id, $online_num);
        }
    }
}
